package servlet.stu;

import bean.Hol;
import bean.PageRequest;
import bean.Result;
import bean.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import config.Public;
import dao.UserDaoImpl;
import service.UserService;
import service.UserServiceImpl;
import utils.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;



//学生注册
@WebServlet("/stu/sign")
public class StuSignServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("===============学生注册页面=============");

        String stuName = request.getParameter("stuName");
        String password = request.getParameter("password");
        int stuId = Integer.parseInt(request.getParameter("stuId"));
        String phone = request.getParameter("phone");
        List<Student> lists = findAllStu();
        for(Student student:lists)
        {
            if(stuId == student.getStuId())
            {
                Result result= new Result();
                result.fail("学号冲突");
                ObjectMapper objectMapper = new ObjectMapper();
                String str = objectMapper.writeValueAsString(result);
                response.setContentType("application/json;charset=utf8");
                response.getWriter().print(str);
                return;
            }
        }

        UserService userService = new UserServiceImpl();
        Student student=new Student();
        try {
            student.setStuName(stuName);
            student.setPhone(phone);
            student.setPassword(password);
            student.setStuId(stuId);
            Public.distribution(student);
            userService.isSignSuccess(student);
        }catch (Exception e) {
            Result result= new Result();
            result.fail(e.toString());
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);
        }
        Result result = new Result();
        ObjectMapper objectMapper = new ObjectMapper();
            student = new UserDaoImpl().findByPasswordAndId(password,stuId);

            result.success(student);
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);

        }

    private List<Student> findAllStu() {
        PageRequest pageRequest = new PageRequest();
        Connection connection=null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from stu where  delete_flag=0 ";
            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();
            List<Student> hols = new ArrayList<>();
            while (resultSet.next()){
                Student student=new Student();
                student.setStuId(resultSet.getInt("stu_id"));

                hols.add(student);

            }

            return hols;


        } catch (SQLException e) {
            System.out.println(e);
            throw new RuntimeException(e);
        }
    }

}
